package com.yiwei.zhh.view;

import android.content.Context;
import android.content.res.Configuration;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.MiBaoQuestionV3Adapter;
import com.yiwei.zhh.adapter.MoneyUseV3Adapter;
import com.yiwei.zhh.model.DealTypesV3Model;
import com.yiwei.zhh.model.MiBaoQuestionModel;
import com.yiwei.zhh.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class MiBaoQuestionDialog extends android.app.Dialog {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.list)
    RecyclerView list;


    private Context context;
    private MiBaoQuestionV3Adapter adapter;
    private boolean initFinish = false;

    public MiBaoQuestionDialog(Context context) {
        super(context, R.style.mydialog);
        this.context = context;
        init();
    }

    public MiBaoQuestionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public MiBaoQuestionDialog setOnSelect(OnSureListener listener) {
        selectListener = listener;
        return this;
    }

    @Override
    public void show() {
        super.show();
    }

    public void setData(List<MiBaoQuestionModel.DataBean> item) {
        adapter.update(item);
    }


    public interface OnSureListener {
        void onSure(String id, String name);
    }

    private OnSureListener selectListener;

    private void init() {
        Window window = this.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.mydialog_animation);
        setContentView(R.layout.dialog_select_xuexin_question_v3);
        ButterKnife.bind(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 8;
        }
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(false);

        adapter = new MiBaoQuestionV3Adapter(context);
        adapter.setOnChooseListener(new MiBaoQuestionV3Adapter.OnChooseListener() {
            @Override
            public void onClick(String id, String name) {
                if (selectListener != null) {
                    selectListener.onSure(id, name);
                }
                dismiss();
            }
        });
        list.setAdapter(adapter);
    }

//    public void getUserPurpose() {
//        if (!Network.checkNetwork(context)) {
//            ToastUtil.makeText(context, "网络连接失败").show();
//            return;
//        }
//        progressBar.setVisibility(View.VISIBLE);
//        Api.api_service.getDealuse().compose(RxLifeUtil.checkOn(context)).
//                doFinally(() -> {
//                    try {
//                        progressBar.setVisibility(View.GONE);
//                    } catch (NullPointerException e) {
//
//                    }
//                }).
//                subscribe(result -> {
//                    adapter.update(result.data);
//                    if (adapter.getItemCount() != 0) {
//                        initFinish = true;
//                    }
//                });
//    }

}
